<template>
  <div class="order-product-page">
    <van-nav-bar
      title="选择商品"
      left-arrow
      @click-left="router.back()"
    />

    <div class="content">
      <!-- 左侧分类栏 -->
      <div class="category-sidebar">
        <div
          v-for="(category, index) in categories"
          :key="category.id"
          class="category-item"
          :class="{ active: activeCategory === index }"
          @click="changeCategory(index)"
        >
          {{ category.name }}
        </div>
      </div>

      <!-- 右侧商品列表 -->
      <div class="product-content">
        <!-- 当前分类标题 -->
        <div class="category-title">
          <div class="title-text">{{ categories[activeCategory]?.name || '商品列表' }}</div>
          <div class="product-count">{{ filteredProducts.length }}个商品</div>
        </div>

        <!-- 商品列表 -->
        <div class="product-list">
          <div v-for="product in filteredProducts" :key="product.id" class="product-item">
            <div class="product-card">
              <img :src="product.image" class="product-image" />
              <div class="product-info">
                <div class="product-name">{{ product.name }}</div>
                <div class="product-desc">{{ product.description }}</div>
                <div class="product-price-action">
                  <div class="product-price">¥{{ product.price.toFixed(2) }}</div>
                  <van-button
                    round
                    type="primary"
                    size="small"
                    icon="plus"
                    class="add-button"
                    @click="addToCart(product)"
                  />
                </div>
              </div>
            </div>
          </div>

          <!-- 空状态 -->
          <div v-if="filteredProducts.length === 0" class="empty-state">
            <van-empty description="暂无商品" />
          </div>
        </div>
      </div>
    </div>

    <!-- 购物车栏 -->
    <div class="cart-bar" v-if="cartItems.length > 0">
      <div class="cart-info">
        <div class="cart-total">
          <span class="total-label">合计：</span>
          <span class="total-price">¥{{ totalPrice.toFixed(2) }}</span>
        </div>
        <div class="cart-count">共{{ totalCount }}件商品</div>
      </div>
      <van-button round type="primary" class="submit-button" @click="submitOrder">
        确认下单
      </van-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { showToast } from 'vant'

const router = useRouter()
const route = useRoute()

// 分类数据
interface Category {
  id: number;
  name: string;
}

// 商品数据
interface Product {
  id: number;
  name: string;
  price: number;
  image: string;
  description: string;
  categoryId: number;
}

// 购物车商品
interface CartItem {
  id: number;
  productId: number;
  name: string;
  price: number;
  quantity: number;
  image: string;
}

// 模拟分类数据
const categories = ref<Category[]>([
  { id: 1, name: '饮料' },
  { id: 2, name: '零食' },
  { id: 3, name: '日用品' },
  { id: 4, name: '当季新品' },
  { id: 5, name: '今日特价' },
  { id: 6, name: '联名套餐' }
])

// 模拟商品数据
const allProducts = ref<Product[]>([
  {
    id: 1,
    name: '鲜萃咖啡',
    price: 13.9,
    image: '/products/coffee1.png',
    description: '鲜萃现泡 0植脂末',
    categoryId: 1
  },
  {
    id: 2,
    name: '生椰拿铁',
    price: 13.9,
    image: '/products/coffee2.png',
    description: '4年突破12亿杯 原创YYDS!',
    categoryId: 1
  },
  {
    id: 3,
    name: '橙C美式',
    price: 13.9,
    image: '/products/coffee3.png',
    description: '0脂 | 一杯橙汁含量≈1个橙子',
    categoryId: 1
  },
  {
    id: 4,
    name: '轻咖柠檬茶',
    price: 13.9,
    image: '/products/tea1.png',
    description: '0脂|广东香水柠檬|含生豆精华',
    categoryId: 4
  },
  {
    id: 5,
    name: '爆浆曲奇',
    price: 8.0,
    image: '/products/snack1.png',
    description: '香脆可口 浓郁奶香',
    categoryId: 2
  },
  {
    id: 6,
    name: '软欧包',
    price: 9.5,
    image: '/products/snack2.png',
    description: '松软可口 早餐必备',
    categoryId: 2
  }
])

// 当前激活的分类索引
const activeCategory = ref(0)

// 购物车商品
const cartItems = ref<CartItem[]>([])

// 根据当前分类筛选商品
const filteredProducts = computed(() => {
  const categoryId = categories.value[activeCategory.value]?.id
  if (!categoryId) return []
  return allProducts.value.filter(p => p.categoryId === categoryId)
})

// 计算购物车总价
const totalPrice = computed(() => {
  return cartItems.value.reduce((total, item) => total + item.price * item.quantity, 0)
})

// 计算购物车商品总数
const totalCount = computed(() => {
  return cartItems.value.reduce((total, item) => total + item.quantity, 0)
})

// 切换分类
const changeCategory = (index: number) => {
  activeCategory.value = index
}

// 添加商品到购物车
const addToCart = (product: Product) => {
  const existingItem = cartItems.value.find(item => item.productId === product.id)
  
  if (existingItem) {
    existingItem.quantity += 1
  } else {
    cartItems.value.push({
      id: Date.now(),
      productId: product.id,
      name: product.name,
      price: product.price,
      quantity: 1,
      image: product.image
    })
  }
  
  showToast({
    type: 'success',
    message: '已添加到购物车',
    position: 'bottom'
  })
}

// 提交订单
const submitOrder = () => {
  if (cartItems.value.length === 0) {
    showToast('请先添加商品')
    return
  }
  
  // TODO: 跳转到订单确认页面
  console.log('提交订单', cartItems.value)
  showToast('提交订单成功')
}

onMounted(() => {
  console.log('订单商品选择页面初始化')
})
</script>

<style lang="scss" scoped>
.order-product-page {
  min-height: 100vh;
  background: #f7f8fa;
  display: flex;
  flex-direction: column;
}

.content {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - 46px - 60px); // 减去导航栏和底部购物车栏的高度
}

.category-sidebar {
  width: 90px;
  background: #f5f5f5;
  overflow-y: auto;
  
  .category-item {
    padding: 16px 12px;
    text-align: center;
    font-size: 14px;
    color: #323233;
    position: relative;
    
    &.active {
      background: #fff;
      color: #1989fa;
      font-weight: 500;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 3px;
        height: 16px;
        background: #1989fa;
      }
    }
  }
}

.product-content {
  flex: 1;
  overflow-y: auto;
  background: #fff;
  
  .category-title {
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f5f5f5;
    
    .title-text {
      font-size: 16px;
      font-weight: 500;
    }
    
    .product-count {
      font-size: 12px;
      color: #969799;
    }
  }
  
  .product-list {
    padding: 12px;
    
    .product-item {
      margin-bottom: 12px;
    }
    
    .product-card {
      display: flex;
      align-items: center;
      padding: 12px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
    }
    
    .product-image {
      width: 80px;
      height: 80px;
      object-fit: cover;
      border-radius: 4px;
      margin-right: 12px;
    }
    
    .product-info {
      flex: 1;
    }
    
    .product-name {
      font-size: 16px;
      font-weight: 500;
      margin-bottom: 4px;
    }
    
    .product-desc {
      font-size: 12px;
      color: #969799;
      margin-bottom: 8px;
    }
    
    .product-price-action {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .product-price {
      color: #ee0a24;
      font-size: 16px;
      font-weight: 500;
    }
    
    .add-button {
      width: 28px;
      height: 28px;
      padding: 0;
      
      .van-icon {
        font-size: 18px;
      }
    }
  }
  
  .empty-state {
    padding: 40px 0;
  }
}

.cart-bar {
  height: 60px;
  background: #fff;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  
  .cart-info {
    .cart-total {
      .total-label {
        font-size: 14px;
      }
      
      .total-price {
        font-size: 18px;
        font-weight: 500;
        color: #ee0a24;
      }
    }
    
    .cart-count {
      font-size: 12px;
      color: #969799;
    }
  }
  
  .submit-button {
    height: 36px;
    min-width: 100px;
  }
}
</style> 